package com.wy.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.wy.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: wy
 * @Date:2019/10/29 16:27
 */
    @RestController
    @Slf4j
    @DefaultProperties(defaultFallback = "defaultFallback")
    @RequestMapping("/consumer")
public class ConsumerController {

        @Autowired
         private RestTemplate restTemplate;
        @Autowired
         private DiscoveryClient discoveryClient;
         @RequestMapping("/{id}")
         @HystrixCommand
        public User getUser(@PathVariable Long id){
             //String url="http://localhost:9010/user/"+id;
            /* List<ServiceInstance> user_service = discoveryClient.getInstances("user_service");
             ServiceInstance serviceInstance = user_service.get(0);
             return restTemplate.getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/user/"+id,User.class);*/
             String url="http://user-service/user/"+id;//user-service相当于127.0.0.1:9010或9011
             return  restTemplate.getForObject(url,User.class);
        }
               @GetMapping("/findAll")
         public List<User> findAll(){
                  /* List<ServiceInstance> user_service = discoveryClient.getInstances("user_service");
                   ServiceInstance serviceInstance = user_service.get(0);*/
                  // User[] users = restTemplate.getForObject("http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/user/findAll", User[].class);
                     String url="http://user-service/user/findAll";
                   User[] users = restTemplate.getForObject(url, User[].class);
                   return Arrays.asList(users);
               }

                @GetMapping("/delete/{id}")
               public String delete(@PathVariable String id){
                   /* List<ServiceInstance> user_service = discoveryClient.getInstances("user_service");
                    ServiceInstance serviceInstance = user_service.get(0);*/
                     restTemplate.delete("http://user-service/user/delete/{}",id);
                    // restTemplate.delete("http://localhost:9010/user/delete/12");

                     return "删除成功!";
               }
               @RequestMapping("/update")
               public String update(User user){
                   /* List<ServiceInstance> user_service = discoveryClient.getInstances("user_service");
                    ServiceInstance serviceInstance = user_service.get(0);*/
                   restTemplate.put("http://localhost:9010/user/update/",null);
                     return "修改成功!";
               }

    public String queryByIdFallback(Long id){
        log.error("查询用户信息失败。id：{}", id);
        return "对不起，网络太拥挤了！";
    }

    public String defaultFallback(){
        return "默认提示：对不起，网络太拥挤了！";
    }
}
